// import { login, logout, getInfo } from '@/api/user'
import { getUserInfo, login } from '@/api/user'
import { constantRoutes, resetRouter } from '@/router'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

const state = {
  token: getToken() || '',
  userInfo: {}, // 用户信息
  routes: constantRoutes // 用户所有的路由规则
}

const mutations = {
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes] // 静态路由规则+自己的动态路由规则
  },
  setToken: (state, token) => {
    state.token = token
    setToken(token)
  },
  removeToken: (state) => {
    state.token = ''
    removeToken()
  },
  setUserInfo: (state, userInfo) => {
    state.userInfo = userInfo
  },
  removeUserInfo: (state) => {
    state.userInfo = {}
  }

}
const actions = {
  async login(context, obj) {
    const res = await login(obj)
    console.log(res)
    context.commit('setToken', res)
  },
  async getUserInfo(context) {
    const result = await getUserInfo()
    console.log(result)
    context.commit('setUserInfo', result)
    return result
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
    // 重置路由规则=>下一个账号登进来不应该是基于上一个页面的规则
    resetRouter()
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

